package com.fernandomdo.ServiceAdministrator;

// Android Imports
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

import com.fernandomdo.ServiceAdministrator.databaseInterface.DatabaseInterface;

public class AddTerritory extends Activity implements OnClickListener{
	private DatabaseInterface databaseInterface;
	private Context appContext;
	
	EditText territoryName;
	EditText description;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_territory);
		
		appContext = getApplicationContext();
		databaseInterface = new DatabaseInterface(appContext);
		
		territoryName = (EditText) findViewById(R.id.territory_name);
		
		description = (EditText) findViewById(R.id.description);
		description.setText("Enter a simple description of the territory here...");
		description.selectAll();
		
		View addButton = findViewById(R.id.add_button);
		addButton.setOnClickListener(this);	
		View cancelButton = findViewById(R.id.cancel_button);
		cancelButton.setOnClickListener(this);
	}
	
	/**
	 * Create a better looking Toast message...
	 */
	@Override
	public void onClick(View v) {
		int duration = 5;
		CharSequence text;
		
		switch(v.getId()) {
			case R.id.add_button:
				if(databaseInterface.addTerritory(territoryName.getText().toString(), description.getText().toString()) == true) {
					text = "The territory was added!";
				}				
				else {
					text = "That territory already exists! You can edit it instead.";
				}
				Toast.makeText(appContext, text, duration).show();
				finish();
	    		break;
			case R.id.cancel_button:
				finish();
				break;
	    	}
	    }	
}